package com.dwxt.cdms.api.controller;

import com.dwxt.cdms.api.common.utils.StringUtil;
import com.dwxt.cdms.api.service.ServiceService;
import com.dwxt.cdms.entity.info.ServiceDto;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.util.IDUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cdms/service")
public class ServiceContrller {

    @Autowired
    private ServiceService serviceService;

    @PostMapping("/insertOrUpdate")
    private BaseResult insertOrUpdate(ServiceDto service){
        if (service.getId()!=null){
            serviceService.update(service);
            return BaseResult.ok();
        }else {
            service.setId(IDUtils.genItemId());
            serviceService.insert(service);
            return BaseResult.ok();
        }
    }

    @GetMapping("/delete")
    private BaseResult delete(String id){
        if (id!=null){
            //还未删除视频物理地址，需要开发
            int delete = serviceService.delete(id);
            if (delete>0){
                return BaseResult.ok();
            }else {
                return BaseResult.build(StatusCode.FAIL);
            }
        }
        return BaseResult.build(StatusCode.PARAMETER_WRONG);
    }

    @GetMapping("/selectById")
    private BaseResult selectById(String id){
        if (StringUtil.isNotEmpty(id)){
            ServiceDto service = serviceService.selectById(id);
            return BaseResult.ok(service);
        }
        return BaseResult.build(StatusCode.PARAMETER_WRONG);
    }

    @GetMapping("/selectList")
    private BaseResult selectList(@RequestParam(required = true, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = true, defaultValue = "10") Integer pageSize,
                                  @RequestParam(required = false) String typeId){
        PageHelper.startPage(pageNum,pageSize);
        List<ServiceDto> services = null;
        if (typeId!=null){
            services = serviceService.getServiceListByType(typeId);
        }else {
            services = serviceService.selectByObj();
        }
        return BaseResult.ok(new PageInfo<>(services));
    }
}
